﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NORM.DataBase
{
    /// <summary>
    /// DataBase 实例创建工厂
    /// </summary>
    public class DataBaseFactory
    {
        private static DataBase _db;
        private static object lockobj;

        /// <summary>
        /// 创建 DataBase 实例
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="dbType">数据库类型枚举</param>
        /// <returns>DataBase 实例</returns>
        public static DataBase Create(string connectionString, DataBaseTypes dbType)
        {
            DataBase db = null;            
            switch (dbType)
            {
                case DataBaseTypes.SqlDataBase:
                    db = new SqlDataBase(connectionString);
                    break;
                case DataBaseTypes.SqliteDataBase:
                    db = new SQLiteDataBase(connectionString);
                    break;
                case DataBaseTypes.PostgreSqlDataBase:
                    db = new PostgreSqlDataBase(connectionString);
                    break;
                default:
                    db = new SQLiteDataBase(connectionString);
                    break;
            }
            return db;
        }

        /// <summary>
        /// 创建 DataBase 实例
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static DataBase Create(string name)
        {
            DataBase db = null;
            string providerName = System.Configuration.ConfigurationManager.ConnectionStrings[name].ProviderName;
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[name].ConnectionString;

            switch (providerName)
            {
                case "System.Data.SQL":
                    db = new SqlDataBase(connectionString);
                    break;
                case "System.Data.SQLite":
                    db = new SQLiteDataBase(connectionString);
                    break;
                case "System.Data.PostgreSql":
                    db = new PostgreSqlDataBase(connectionString);
                    break;              
                default:
                    db = new SQLiteDataBase(connectionString);
                    break;
            }

            return db;
        }

        /// <summary>
        /// 创建 DataBase 实例
        /// </summary>
        /// <returns></returns>
        public static DataBase Create()
        {
            DataBase db = null;
            string providerName = System.Configuration.ConfigurationManager.ConnectionStrings["default"].ProviderName;
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString;

            switch (providerName)
            {
                case "System.Data.SQL":
                    db = new SqlDataBase(connectionString);
                    break;
                case "System.Data.SQLite":
                    db = new SQLiteDataBase(connectionString);
                    break;
                case "System.Data.PostgreSql":
                    db = new PostgreSqlDataBase(connectionString);
                    break;                
                default:
                    db = new SQLiteDataBase(connectionString);
                    break;
            }

            return db;
        }

        /// <summary>
        /// 获取配置文件 WebConfig / AppConfig 里 default 默认配置连接字符串
        /// </summary>
        public static DataBase Default
        {
            get
            {
                if (_db == null)
                {
                    lockobj = new object();
                    lock (lockobj)
                    {
                        _db =Create();
                    }
                }
                return _db;
            }
        }
    
    }
}
